export const getmark = () => {
  const setWatermark = (str: any) => {
    const id = "1.23452384164.123412416";
    if (document.getElementById(id) !== null) {
      document.body.removeChild(document.getElementById(id)!);
    }
    //创建一个画布
    const can = document.createElement("canvas");
    const cans = can.getContext("2d")!;

    // 设置字体以便测量文字宽度
    cans.font = "18px Vedana";

    // 测量文字宽度
    const textMetrics = cans.measureText(str);
    const textWidth = textMetrics.width;

    // 动态置画布大小，确保文字完全显示
    const padding = 40; // 增加内边距
    can.width = Math.max(200, textWidth + padding * 2); // 最小宽度200px
    can.height = 80; // 适当的高度

    // 重新设置字体（画布大小改变后需要重新设置）
    cans.font = "18px Vedana";
    //设置填充绘画的颜色、渐变或者模式
    // 创建白色渐变
    const gradient = cans.createLinearGradient(0, 0, can.width, can.height);
    gradient.addColorStop(0, "rgba(255, 255, 255, 0.6)");
    gradient.addColorStop(0.5, "rgba(255, 255, 255, 0.4)");
    gradient.addColorStop(1, "rgba(255, 255, 255, 0.2)");
    cans.fillStyle = gradient;
    //设置文本内容的当前对齐方式
    cans.textAlign = "center";
    //设置文本基线
    cans.textBaseline = "middle";

    //旋转角度
    cans.translate(can.width / 2, can.height / 2);
    cans.rotate((-15 * Math.PI) / 180);

    //在画布中心绘制文字
    cans.fillText(str, 0, 0);

    const div = document.createElement("div");

    div.id = id;

    div.style.pointerEvents = "none";

    div.style.top = "30px";

    div.style.left = "0px";

    div.style.position = "fixed";

    div.style.zIndex = "10";

    div.style.width = document.documentElement.clientWidth + "px";

    div.style.height = document.documentElement.clientHeight + "px";

    div.style.background =

      "url(" + can.toDataURL("image/png") + ") left top repeat";

    document.body.appendChild(div);

    return id;

  };
  // 该方法只允许调用一次
  const watermark = (str: string) => {
    let id = setWatermark(str);
    setInterval(() => {
      if (document.getElementById(id) === null) {
        id = setWatermark(str);
      }
    }, 500);
    window.onresize = () => {
      setWatermark(str);
    };
  };
  return { watermark };

};

export default getmark;
